package com.leyou.user.config;

import com.leyou.common.auth.utils.RsaUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.security.PublicKey;

/**
 * @author Eric
 * @version v1.0
 * @package com.leyou.auth.config
 * @date 2019/7/7 18:51
 * @description:
 **/
@Slf4j
@Data
@ConfigurationProperties("ly.jwt")
public class JwtProperties implements InitializingBean {
    /**
     * 公钥地址
     */
    private String pubKeyPath;

    /**
     * 公钥对象
     */
    private PublicKey publicKey;

    /**
     * 注入user对象
     */
    private UserTokenProperties user =new UserTokenProperties();

    @Data
    public class UserTokenProperties{


        /**
         * 存放token的cookie名称
         */
        private String cookieName;



    }
    private PrivilegeTokenProperties app =new PrivilegeTokenProperties();

    @Data
    public class PrivilegeTokenProperties{

        private Long id;
        private String secret;
        private String headerName;
    }


    @Override

    public void afterPropertiesSet() {

        try {
            this.publicKey = RsaUtils.getPublicKey(pubKeyPath);
        } catch (Exception e) {
            log.error("【用户中心】密钥加载失败，原因{}", e.getMessage(), e);
            throw new RuntimeException(e);
        }


    }
}
